﻿using Sasc.PV.BLL.Annotation;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sasc.PV.BLL.ZhuDing
{
    [GongXu("返工", typeof(ZhuDingCheJianManagement))]
    public class ZhuDingFanGongGongXu : ZhuDingGongXuBase, ICheJianFanGongGongXu<ZhuDingCheJianManagement, ZhuDingGongXuTarget>
    {
        #region Statis ReadOnly Fields
        static readonly string FieldName_FanGongVersion = DbMappingHelper.GetDbFieldName<IPvGongXuFanGongModel, int>(c => c.FanGongVersion);
        static readonly string FieldName_GanGuoBianHao = DbMappingHelper.GetDbFieldName<PvZDFanGong, string>(c => c.GanGuoBianHao);
        #endregion

        #region Implement ICheJianFanGongGongXu
        public int GetCurrentFanGongVersion(ZhuDingGongXuTarget target)
        {   
            // 因为坩埚编码是必须的，但是小锭不是必须的，所以只需要判断坩埚就行
            var sqlBuilder = new StringBuilder();
            sqlBuilder.AppendFormat("SELECT MAX({2}) FROM {0} WHERE {3}='{1}'", 
                                    GetGongXuDbTableName(), 
                                    target.GanGuoBianHao,
                                    FieldName_FanGongVersion,
                                    FieldName_GanGuoBianHao);
            return (int)(DbHelperSQL.GetSingle(sqlBuilder.ToString()) ?? 0);
        }

        public int GetCurrentFanGongVersion(ICheJianGongXuTarget target)
        {
            return GetCurrentFanGongVersion(target as ZhuDingGongXuTarget);
        }
        #endregion 
    }
}
